package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.utils.EnvSetup;

public class YmtCouponWapper {
    SqlManager manager;

    public YmtCouponWapper() {
        manager = new SqlManager("ymt" + EnvSetup.getEnv());
    }

    public List<YmtCoupon> selectBysCouponCode(String sCouponCode) {
        List<YmtCoupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectBysCouponCode",
                        sCouponCode);
        manager.commit();
        return result;
    }

    public List<Map> selectCouponInfoByCouponCode(String sCouponCode) {
        Map m = new HashMap();
        m.put("sCouponCode", sCouponCode);
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectCouponInfoByCouponCode",
                        m);
        manager.commit();
        return result;
    }


    // 根据短码，用户id，查询短码公共优惠券使用记录
    public List<Map> selectYmtCouponPublicUsed(String couponCode, int userId) {
        Map m = new HashMap();
        m.put("couponCode", couponCode);
        m.put("userId", userId);
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectYmtCouponPublicUsed",
                        m);
        manager.commit();
        return result;
    }

    public List<YmtCoupon> selectByiBatchId(int iBatchId) {
        List<YmtCoupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectByiBatchId",
                        iBatchId);
        manager.commit();
        return result;
    }


    public void setdValidStart(Date date, String sCouponCode) {
        Map m = new HashMap();
        m.put("date", date);
        m.put("sCouponCode", sCouponCode);
        manager.update(
                "com.ymttest.database.mapping.YmtCouponMapper.setdValidStart",
                m);
        manager.commit();
    }

    public void setdValidEnd(Date date, String sCouponCode) {
        Map m = new HashMap();
        m.put("date", date);
        m.put("sCouponCode", sCouponCode);
        manager.update(
                "com.ymttest.database.mapping.YmtCouponMapper.setdValidEnd",
                m);
        manager.commit();
    }

    public YmtCoupon selectTop1() {
        YmtCoupon result = (YmtCoupon) manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1",
                        null).get(0);
        return result;
    }
    
    public List<YmtCoupon> selectTop5() {
    	List<YmtCoupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop5",
                        null);
        return result;
    }

    public List<Map> selectTop1InValidPlatformShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1InValidPlatformShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1InValidPlatformLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1InValidPlatformLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1InValidSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1InValidSellerCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ExpirePlatformShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1ExpirePlatformShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ExpirePlatFormLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1ExpirePlatFormLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ExpireSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1ExpireSellerCouponInfo",
                        m);        
        manager.commit();
        return result;
    }

    public List<Map> selectTop1NotStartSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1NotStartSellerCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1NotStartPlatFormShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1NotStartPlatFormShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1NotStartPlatformLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1NotStartPlatformLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ValidPlatformShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1ValidPlatformShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ValidPlatformLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1ValidPlatformLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ValidSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponMapper.selectTop1ValidSellerCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public int useCoupon(int userId, String couponCode, Integer mainOrderId) {
        Map m = new HashMap();
        m.put("couponCode", couponCode);
        m.put("userId", userId);
        m.put("mainOrderId", mainOrderId);
        Integer i = manager.session.selectOne("com.ymttest.database.mapping.YmtCouponMapper.useCoupon", m);
        manager.commit();
        return i;
    }

    public int useCouponV2(int userId, String couponCode, Integer mainOrderId) {
        Map m = new HashMap();
        m.put("couponCode", couponCode);
        m.put("userId", userId);
        m.put("mainOrderId", mainOrderId);
        Integer i = manager.session.selectOne("com.ymttest.database.mapping.couponMapper.useCoupon", m);
        manager.commit();
        return i;
    }
    public static void main(String[] args) {
        YmtCouponWapper test = new YmtCouponWapper();

        System.out.println(test.useCoupon(20238699, "AIL7PT3W1",null));
        // test.insertYmtCouponSetting(3,11);
        // Logger.debug(test.getLastYmtCouponSettingId());


    }
}
